home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dmake / convert.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  2KB  |  118 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  CONVERT.C
  9.  */
  10.  
  11. #include "defs.h"
  12.  
  13. #define MAXLEVELS   10
  14.  
  15. Prototype int  WildConvert(char *, char *, char *, char *);
  16.  
  17. /*
  18.  *  Run srcBuf through the srcMat pattern matcher and if it matches
  19.  *  convert it via dstMat into dstBuf, else dstBuf \0 len.
  20.  */
  21.  
  22.  
  23. int
  24. WildConvert(srcBuf, dstBuf, srcMat, dstMat)
  25. char *srcBuf;
  26. char *dstBuf;
  27. char *srcMat;
  28. char *dstMat;
  29. {
  30.     short r = 0;
  31.     long i;
  32.     static short Index;
  33.     static short SubLen[MAXLEVELS];
  34.     static char *SubStr[MAXLEVELS];
  35.  
  36.     if (Index == MAXLEVELS)
  37.     error(FATAL, "maximum recursion reached in WildConvert");
  38.  
  39.     db4printf(("WildConvert %-15s (%s -> %s)\n", srcBuf, srcMat, (dstMat) ? dstMat : "..."));
  40.  
  41.     /*
  42.      *    skip non-wildcards, srcBuf must match srcMat
  43.      */
  44.  
  45.     while (*srcMat && *srcMat != '*' && *srcMat != '?') {
  46.     if (*srcBuf != *srcMat)
  47.         return(-1);
  48.     ++srcBuf;
  49.     ++srcMat;
  50.     }
  51.  
  52.     switch(*srcMat) {
  53.     case '\0':                      /*  match end, terminating case */
  54.     if (*srcBuf)                /*  buf srcBuf not exhausted!   */
  55.         r = -1;
  56.     break;
  57.     case '?':                       /*  match 1 */
  58.     if (*srcBuf == 0)           /*  match failed against srcbuf  */
  59.         return(-1);
  60.     SubStr[Index] = srcBuf;
  61.     SubLen[Index] = 1;
  62.     ++Index;
  63.     r = WildConvert(srcBuf + 1, NULL, srcMat + 1, NULL);
  64.     --Index;
  65.     break;
  66.     case '*':                       /*  match any   */
  67.     /*
  68.      *  strangeness in loop is so \0 (nil string) is checked for,
  69.      *  it is perfectly valid for the remainder to be nil.
  70.      *
  71.      *  note: bug in NeXT's GCC -O/-O2, had to reorder r == -1 to
  72.      *  the right side of the &&
  73.      */
  74.  
  75.     r = -1;
  76.     for (i = 0; (i == 0 || srcBuf[i-1]) && r == -1; ++i) {
  77.         SubStr[Index] = srcBuf;
  78.         SubLen[Index] = i;
  79.         ++Index;
  80.         r = WildConvert(srcBuf + i, NULL, srcMat + 1, NULL);
  81.         --Index;
  82.     }
  83.     break;
  84.     }
  85.     if (r == 0 && dstMat) {
  86.     short k = 0;
  87.     short n = -1;
  88.  
  89.     while (*dstMat) {
  90.         switch(*dstMat) {
  91.         case '%':
  92.         n = (dstMat[1] - '1');
  93.         case '*':
  94.         case '?':
  95.         if (*dstMat == '%')
  96.             ++dstMat;
  97.         else
  98.             n = k++;
  99.  
  100.         if (n >= 0 && n < MAXLEVELS) {
  101.             movmem(SubStr[n], dstBuf, SubLen[n]);
  102.             dstBuf += SubLen[n];
  103.         }
  104.         break;
  105.         default:
  106.         *dstBuf++ = *dstMat;
  107.         break;
  108.         }
  109.         ++dstMat;
  110.     }
  111.     *dstBuf = 0;
  112.     }
  113.     db4printf((" r = %d\n", r));
  114.     return(r);
  115. }
  116.  
  117.  
  118.